<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Request;
class Node extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\NodeModel();
        $this->validate=new \app\admin\validate\NodeValidate();
    }
    public function index(){
        $pid=Request::param('pid',0,"intval");
        $parent=$this->model->find($pid);
        $pagetitle=$parent?$parent['title']:"菜单管理";        
        $data=$this->model->where("pid",$pid)->order('sort asc')->select();   
        View::assign([
            'ppid' => $parent?$parent['pid']:0,
            'pid' => $pid,
            'pagetitle' => $pagetitle,
            'data'  => $data,
        ]);         
        return View::fetch();
    }
    //设置状态是否启用
    public function setStatus(){
        $id=Request::param('id',0,"intval");
        $status=Request::param('status',0,"intval");
        $this->model->update(['id'=>$id,'status'=>$status]);
        //记录日志
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id);        
        $this->success("设置成功");
    }
    //更新排序
    public function updateSort(){
        $id=Request::param('id');
        $sort=Request::param('sort');
        foreach($id as $key=>$v){
            $this->model->update(['id'=>$id[$key],'sort'=>$sort[$key]]);
        }
        $this->success("设置成功");
    }
    public function add(){
        if(request()->isPost()){
            //获取数据并验证
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            } 
            //增加数据
            $data['status']=isset($data['status'])?1:0;
            $data['level']=$this->model->where("id",$data['pid'])->value("level")+1;
            $this->model->save($data);
            $lastId=$this->model->id;
            //记录日志
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
            $this->success("添加成功");
        }else{
            //获取最大的排序 
            $pid=Request::param('pid');
            $sort=$this->model->where("pid",$pid)->max("sort")+10;
            View::assign("sort",$sort);
            View::assign("pid",$pid);
            return View::fetch();
        }
    }      
    public function update(){
        if(request()->isPost()){
            //获取数据并验证
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }            
            //数据更新
            $data['status']=isset($data['status'])?1:0;
            $this->model->update($data);
            //记录日志
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data["id"],$this->request->param());
            $this->success("修改成功");            
        }else{
            $id=Request::param('id');
            $r=$this->model->find($id);
            View::assign("r",$r);
            return View::fetch();
        }
    }  
    //删除节点
    public function delete(){
        $id=Request::param('id');
        $count=$this->model->where(array("pid"=>$id))->count();        
        if($count>0){
            $this->error("该节点下有子节点，如要删除请先删除子节点");
        }else{            
            if($this->model::destroy($id)){
                //记录日志
                $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id,$this->request->param());
                $this->success("删除成功");
            }else{
                $this->error("删除失败");
            }                
        }
    }       
    //icon
    public function icon(){
        View::assign("pagetitle","ICON图标");
        return View::fetch();
    }    
}
